package com.tn.jinq.event;

import static com.tn.jinq.util.ObjectUtils.*;

/**
 * An object that represents a change to a value.
 */
public class ChangeEvent<T> extends AbstractChangeEvent
{
  private T value;

  /**
   * Creates a new <code>ChangeEvent</code> initialized with the <code>type</code> and <code>value</code>.
   *
   * @param type the type of change.
   * @param value the value of the change.
   */
  public ChangeEvent(ChangeEventType type, T value)
  {
    super(type);
    this.value = value;
  }

  /**
   * Returns the value.
   */
  public T getValue()
  {
    return value;
  }

  /**
   * Compares this <code>ChangeEvent</code> to another object for equality.  For another object to be considered equal
   * to a <code>ChangeEvent</code> the other object must be a <code>ChangeEvent</code> and must have been initialized
   * with an equal <code>type</code> and <code>value</code>.
   */
  @Override
  public boolean equals(Object object)
  {
    return this == object || (object != null && getClass().equals(object.getClass()) &&
      areEqual(getType(), ((ChangeEvent)object).getType()) &&
      areEqual(getValue(), ((ChangeEvent)object).getValue()));
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode()
  {
    int result = getValue() != null ? getValue().hashCode() : 0;
    result = 31 * result + (getType() != null ? getType().hashCode() : 0);
    return result;
  }
}
